﻿/// <reference path="../common/form-base.js" />
!function ($) {
    var $ = fly
    window.indexPath = '../'
    $.box.OutboxForm = $.Class({
        name: '工作',
        title: '办理工作',
        base: $.box.FormBase,
        getModelUrl: $.base.ajaxUrl('work', 'GetAssignToMeWork'),
        buttons: ['<a onclick="page.save();return false">办理完毕</a>', '<a id=to-top style="display:none" href="#top">回到顶部</a>', '<a href="list.htm?m=oa&list=inbox">取消</a>'],
        pageFormat: ['<div id=top-empty></div><div id="title-block"><a name=top>{title}</a></div>',
                    '<a id=to-process href=#process style="position:fixed;right:20px;top:70px;display:none" class=button-32 >办理</a>',
                    '<form id="content-block" class="oa-process-form" method="post"></form>',
                    '<div id="toolbar" class=button-32  ></div><br/>',
                    '<div id="summarys"></div><br/>'].join(''),
        save: function () {
            var summary = this.summaryBox.val()
            if (!summary) {
                if (!confirm('你没有填写办理摘要！\r\n\r\n要继续提交吗？？')) {
                    this.summaryBox.focus();
                    return
                }
            }

            $.post($.box.ajaxUrl('work', 'ProcessWork'), { id: this.modelId, summary: summary }, function (result) {
                result.msg && $.alert(result.msg);
                if (result.success) {
                    window.open('list.htm?m=oa&list=inbox', '_self')
                }
            }, $.common.onAjaxErr);
        },
        createMembers: function () {
            this.dWin = window.parent || window;
            this.indexPath = this.dWin.indexPath || '';
            this.$base.createMembers.apply(this, arguments);
        },
        afterCreateForm: function () {
            var me = this;
            this.$base.afterCreateForm.apply(this, arguments);
            this.attachmentsBox = $("#attachments");
            this.contentBox = $('#content')
            this.summaryBox = $('#summary');
            this.summarysBox = $('#summarys');
            this.showAttachments();
            this.showSummarys();
            this.showContent();
        },
        showContent: function () {
            var me = this;
            if (this.model.content.startWith("FILE:")) {
                var parts = this.model.content.substr(5).split(',')
                this.contentBox.addClass('oa-content-file').html('<iframe src="../file/open-doc.htm?id={0}&name={1}" class="oa-content-frame" frameborder=0 scrolling=no ></iframe>'.format(parts[0], parts[1]))
            }
            else {
                this.contentBox.addClass('oa-content-text').text(this.model.content);
            }
            var cCount = 0
            var handler = setInterval(function () {
                if (cCount++ > 8) {
                    clearInterval(handler)
                }

                if (Math.max(document.documentElement.scrollHeight, document.body.scrollHeight) > 900) {
                    $('#to-top,#to-process').fadeIn()
                    clearInterval(handler)
                }
            }, 2000)
        },
        showAttachments: function () {
            var atts = this.model.attachments
            if (atts && atts[0]) {
                var html = atts.select(function (a) {
                    return '<a href="../file/open-doc.htm?id={id}&name={name}" target=_blank >{name}({0})</a>'.format($.sizeFormat(a.size), a);
                }).join("&nbsp;&nbsp;&nbsp;")
                this.attachmentsBox.html(html)
            }
            else
                this.attachmentsBox.parent().hide();
        },
        showSummarys: function () {
            var html = ('{@for o as s}<div class=summary-info ><span class=summary-time>{s.time.format("yyyy-MM-dd hh:mm")}</span> <span class=summary-user >#{s.user}<span></div>' +
                '<div class=summary-content>{s.summary}</div>{for@}').format(this.model.summarys)
            this.summarysBox.append(html);
        }
    });

    ['../themes/box-default/oa/css.css?' + Math.random(), '../../fly.common/fly.ui/plugins/themes/gray/tags-editor.css'].each($.Style.loadCss);
    $.loadScript('../../fly.common/fly.ui/plugins/input/empty-box.js')
    $.loadScript('../../fly.common/fly.ui/plugins/input/tags-editor.js');
    window.page = new $.box.OutboxForm();
    page.show();
} ()